import { ElMessage, ElMessageBox } from 'element-plus';

export const DeleteMain = (
  right: boolean = false, /* 是否有权限 */
  MainById: Function,     /* 需要执行的函数 */
  row: { id: number, name?: string },
  text: string = '',
  callback: () => void    /* 回调函数 */
) => {
  if (!right) {
    ElMessage.error('您没有该权限！');
    return;
  }
  ElMessageBox.confirm(
    `${ text || `确定要删除 ${ row?.name || '' } 吗？`}`,
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    MainById({ method: 'delete', id: row.id }).then(() => {
      ElMessage.success('删除成功');
      callback();
    }).catch((err: any) => {
      ElMessage.success(`删除失败，${ err }`);
    });
  }).catch(() => {
    ElMessage.info('已取消');
  });
}
export const handleMainSwitch = (
  right: boolean = false, /* 是否有权限 */
  MainById: Function,     /* 需要执行的函数 */
  row: { id: number },
  text: string = '',
  sttr: string,
  sttr_value: any,
  callback: () => void    /* 回调函数 */
) => {
  if (!right) {
    ElMessage.error('您没有该权限！');
    return false;
  }
  return ElMessageBox.confirm(
    `${ text || '确定要切换吗？'}`,
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    MainById({
      method: 'patch',
      id: row.id,
      data: {
        [sttr]: sttr_value,
      }
    }).then(() => {
      ElMessage.success('切换成功');
      callback();
    }).catch((err: any) => {
      ElMessage.success(`切换失败，${ err }`);
    });
  }).catch(() => {
    ElMessage.info('已取消');
  });
}